library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Figure 10.3
summary(factor(df$Q21)) # Iran
summary(factor(df$Q25)) # Transgender ban
summary(factor(df$Q11T)) 

df2 <- df
df2 <- df2[df2$Q11 < 77,]

# Attack Iran treatments
# P_ASSIGN2 == 1 - Control
# P_ASSIGN2 == 4 - Support
# P_ASSIGN2 == 5 - Oppose

# Transgender Ban treatments
# P_ASSIGN3 == 1    # Control
# P_ASSIGN3 == 3    # Support
# P_ASSIGN3 == 2    # Oppose
# P_ASSIGN3 == 4    # Divided

# Q21T
df2$Q21T <- NA
df2$Q21T[df2$Q21 < 3] <- 1
df2$Q21T[df2$Q21 > 2 & df2$Q21 < 77] <- 0

# Q25T
df2$Q25T <- NA
df2$Q25T[df2$Q25 < 3] <- 1
df2$Q25T[df2$Q25 > 2 & df2$Q25 < 77] <- 0

# Survey designs
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )


# Iran
iran <- svyby(~Q21T,~P_ASSIGN2+Q11T, w2_design, svymean, na.rm = TRUE)

# Transgender ban
tban <- svyby(~Q25T,~P_ASSIGN3+Q11T, w2_design, svymean, na.rm = TRUE)

# Build out plots
# Iran
pt.est <- c(iran$Q21T[iran$P_ASSIGN2==1& iran$Q11T==0], 
            iran$Q21T[iran$P_ASSIGN2==4& iran$Q11T==0],
            iran$Q21T[iran$P_ASSIGN2==5& iran$Q11T==0],
            0,
            iran$Q21T[iran$P_ASSIGN2==1& iran$Q11T==1],
            iran$Q21T[iran$P_ASSIGN2==4& iran$Q11T==1],
            iran$Q21T[iran$P_ASSIGN2==5& iran$Q11T==1],
            tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==0], 
            tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==0],
            tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==0],
            tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==0],
            0,
            tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==1], 
            tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==1],
            tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==1],
            tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==1])
ci.low <- c(iran$Q21T[iran$P_ASSIGN2==1& iran$Q11T==0]-1.96*iran$se[iran$P_ASSIGN2==1& iran$Q11T==0], 
            iran$Q21T[iran$P_ASSIGN2==4& iran$Q11T==0]-1.96*iran$se[iran$P_ASSIGN2==4& iran$Q11T==0],
            iran$Q21T[iran$P_ASSIGN2==5& iran$Q11T==0]-1.96*iran$se[iran$P_ASSIGN2==5& iran$Q11T==0],0,
            iran$Q21T[iran$P_ASSIGN2==1& iran$Q11T==1]-1.96*iran$se[iran$P_ASSIGN2==1& iran$Q11T==1],
            iran$Q21T[iran$P_ASSIGN2==4& iran$Q11T==1]-1.96*iran$se[iran$P_ASSIGN2==4& iran$Q11T==1],
            iran$Q21T[iran$P_ASSIGN2==5& iran$Q11T==1]-1.96*iran$se[iran$P_ASSIGN2==5& iran$Q11T==1],
            tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==0]-1.96*tban$se[tban$P_ASSIGN3==1& tban$Q11T==0], 
            tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==0]-1.96*tban$se[tban$P_ASSIGN3==3& tban$Q11T==0],
            tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==0]-1.96*tban$se[tban$P_ASSIGN3==2& tban$Q11T==0],
            tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==0]-1.96*tban$se[tban$P_ASSIGN3==4& tban$Q11T==0],0,
            tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==1]-1.96*tban$se[tban$P_ASSIGN3==1& tban$Q11T==1], 
            tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==1]-1.96*tban$se[tban$P_ASSIGN3==3& tban$Q11T==1],
            tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==1]-1.96*tban$se[tban$P_ASSIGN3==2& tban$Q11T==1],
            tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==1]-1.96*tban$se[tban$P_ASSIGN3==4& tban$Q11T==1])
ci.high <- c(iran$Q21T[iran$P_ASSIGN2==1& iran$Q11T==0]+1.96*iran$se[iran$P_ASSIGN2==1& iran$Q11T==0], 
             iran$Q21T[iran$P_ASSIGN2==4& iran$Q11T==0]+1.96*iran$se[iran$P_ASSIGN2==4& iran$Q11T==0],
             iran$Q21T[iran$P_ASSIGN2==5& iran$Q11T==0]+1.96*iran$se[iran$P_ASSIGN2==5& iran$Q11T==0],0,
             iran$Q21T[iran$P_ASSIGN2==1& iran$Q11T==1]+1.96*iran$se[iran$P_ASSIGN2==1& iran$Q11T==1],
             iran$Q21T[iran$P_ASSIGN2==4& iran$Q11T==1]+1.96*iran$se[iran$P_ASSIGN2==4& iran$Q11T==1],
             iran$Q21T[iran$P_ASSIGN2==5& iran$Q11T==1]+1.96*iran$se[iran$P_ASSIGN2==5& iran$Q11T==1],
             tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==0]+1.96*tban$se[tban$P_ASSIGN3==1& tban$Q11T==0], 
             tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==0]+1.96*tban$se[tban$P_ASSIGN3==3& tban$Q11T==0],
             tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==0]+1.96*tban$se[tban$P_ASSIGN3==2& tban$Q11T==0],
             tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==0]+1.96*tban$se[tban$P_ASSIGN3==4& tban$Q11T==0],0,
             tban$Q25T[tban$P_ASSIGN3==1& tban$Q11T==1]+1.96*tban$se[tban$P_ASSIGN3==1& tban$Q11T==1], 
             tban$Q25T[tban$P_ASSIGN3==3& tban$Q11T==1]+1.96*tban$se[tban$P_ASSIGN3==3& tban$Q11T==1],
             tban$Q25T[tban$P_ASSIGN3==2& tban$Q11T==1]+1.96*tban$se[tban$P_ASSIGN3==2& tban$Q11T==1],
             tban$Q25T[tban$P_ASSIGN3==4& tban$Q11T==1]+1.96*tban$se[tban$P_ASSIGN3==4& tban$Q11T==1])
plot.labs <- c("Control","Support","Oppose","",
               " Control "," Support "," Oppose ",
               "Control ","Support ","Oppose ","Divided "," ",
               "  Control ","  Support ","  Oppose ","  Divided ")
df.plot <- data.frame(Treatment=plot.labs, estimate=pt.est, 
                      lower=ci.low, upper=ci.high)
df.plot$Treatment <- factor(df.plot$Treatment, levels = plot.labs)

g.i <- ggplot(data=df.plot[c(1:7),], 
              aes(x=Treatment, y=estimate),
              size = 0.5)+
  geom_col(position=position_dodge(1), width=0.75,fill="gray50")+
  geom_errorbar(aes(ymin=lower[1:7],ymax=upper[1:7]),width=0.2)+
  ylim(0,.5)+
  ylab("Respondent Agreement (%)")+
  xlab("Not Confident                                                Confident")+
  ggtitle("Iran Strikes")+
  geom_text(aes(label = round(estimate,3)), vjust = -0.5)+
  theme_bw()

g.tb <- ggplot(data=df.plot[c(8:16),], 
               aes(x=Treatment, y=estimate),
               size = 0.5)+
  geom_col(position=position_dodge(1), width=0.75,fill="gray50")+
  geom_errorbar(aes(ymin=lower[8:16],ymax=upper[8:16]),width=0.2)+
  ylim(0,.5)+
  ylab("Respondent Agreement (%)")+
  xlab("Not Confident                                                Confident")+
  ggtitle("Transgender Ban")+
  geom_text(aes(label = round(estimate,3)), vjust = -0.5)+
  theme_bw()

grid.arrange(g.i,g.tb,ncol=2)
